package com.dongdong.android.framework.utils;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by dongdong on 2018/3/27.
 */

public class ThreadUtil {
    private static ExecutorService mExecutor;

    private static ExecutorService mLogExecutor;

    private static ExecutorService mDBExecutor;

    private static ExecutorService mImageExecutor;

    private static ExecutorService mMsgExecutor;

    private static Handler mHandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(Message msg) {
        }
    };

    public static void init() {

        mExecutor = Executors.newFixedThreadPool(10);

        mLogExecutor = Executors.newFixedThreadPool(1);

        mImageExecutor = Executors.newFixedThreadPool(1);

        mDBExecutor = Executors.newFixedThreadPool(1);

        mMsgExecutor = Executors.newFixedThreadPool(1);
    }


    public static void postDelay(Runnable runnable, long time) {
        mHandler.postDelayed(runnable, time);
    }

    public static void removeCallbacks(Runnable runnable) {
        mHandler.removeCallbacks(runnable);
    }

    public static void runOnMainThread(Runnable runnable) {

        if (null == mHandler) {
            synchronized (ThreadUtil.class) {
                if (null == mHandler) {
                    init();
                }
            }
        }

        mHandler.post(runnable);

    }


    public static void runOnWorkThread(Runnable runnable) {

        if (null == mExecutor) {
            synchronized (ThreadUtil.class) {
                if (null == mExecutor) {
                    init();
                }
            }
        }

        mExecutor.execute(runnable);
    }


    public static void runOnDBThread(Runnable runnable) {
        mDBExecutor.execute(runnable);
    }

    public static void runOnMsgThread(Runnable runnable) {
        mMsgExecutor.execute(runnable);
    }


    public static void runOnLogThread(Runnable runnable) {
        mLogExecutor.execute(runnable);
    }


    public static void runOnImageThread(Runnable runnable) {
        mImageExecutor.execute(runnable);
    }


}

